package com.sgb.kjwl.view.ui.adapter.packers;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemOtherAskBinding;
import com.sgb.kjwl.interfaces.RecyclerClick;
import com.sgb.kjwl.model.entity.packers.OtherAskTransmitEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

/**
 * @author : younghow
 * @date : 2021/7/21 17:15
 * description:
 */
public class OtherAskAdapter extends BaseRecycleAdapter<OtherAskTransmitEntity.AddFiled> {

    RecyclerClick<OtherAskTransmitEntity.AddFiled> mClick;

    public void setClick(RecyclerClick<OtherAskTransmitEntity.AddFiled> mClick) {
        this.mClick = mClick;
    }

    public OtherAskAdapter(Context context, List<OtherAskTransmitEntity.AddFiled> data) {
        super(context, R.layout.item_other_ask, data);
    }

    @Override
    public void convert(ItemViewHolder holder, final OtherAskTransmitEntity.AddFiled entity) {
        ItemOtherAskBinding binding = (ItemOtherAskBinding) holder.getmBinding();
        if (!TextUtils.isEmpty(entity.getName())){
            binding.etItemOtherAskTitle.setText(entity.getName());
        }else {
            binding.etItemOtherAskTitle.setText("");
        }

        if (!TextUtils.isEmpty(entity.getContent())) {
            binding.etItemOtherAskContent.setText(entity.getContent());
        }else {
            binding.etItemOtherAskContent.setText("");
        }

        binding.etItemOtherAskTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                datas.get(holder.getIndex_position()).setName(binding.etItemOtherAskTitle.getText().toString());
            }
        });

        binding.etItemOtherAskContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                datas.get(holder.getIndex_position()).setContent(binding.etItemOtherAskContent.getText().toString());
            }
        });

        holder.getItem().findViewById(R.id.iv_item_other_ask_delete).setOnClickListener(v -> {
            binding.etItemOtherAskTitle.setText("");
            binding.etItemOtherAskContent.setText("");
            mClick.includePositionClick(entity,holder.getIndex_position());
        });
    }
}
